package com.example.tumasstest;

import android.bluetooth.BluetoothDevice;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class BleDeviceAdapter extends RecyclerView.Adapter<BleDeviceAdapter.ViewHolder> {
    private static final String TAG = BleDeviceAdapter.class.getName();
    private OnItemClickListener listener;
    private List<BleScanItem> devices;
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    static public class BleScanItem {
        BluetoothDevice device;
        int rssi;

        long timestamp;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public BleDeviceAdapter() {
        super();
        devices = new ArrayList<>();
    }

    public void clearData() {
        devices.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bluetooth_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String name;

        if (devices.size() > 0) {
            try {
                name = devices.get(position).device.getName();
            } catch (SecurityException e) {
                e.printStackTrace();
                name = "null";
            }

            holder.getNameView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(holder.getAdapterPosition());
                    }
                }
            });

            Log.d(TAG, "onBindViewHolder: name:" + name + " rssi:" + devices.get(position).rssi);
            holder.getNameView().setText(name);
            holder.getRssiView().setText(Integer.toString(devices.get(position).rssi));
        }

    }

    public BluetoothDevice getDevice(int position) {
        return devices.get(position).device;
    }

    public void addItem(BluetoothDevice device, int rssi) {
        try {
            boolean found = false;
            int index = 0;
            String name = device.getName();
            if (name == null) {
                name = "null";
            }

            for(int i = 0; i < devices.size(); i++) {
                Log.d(TAG, "target address: " + device.getAddress() + " devices address: " + devices.get(i).device.getAddress());
                if (device.getAddress().equals(devices.get(i).device.getAddress())) {
                    index = i;
                    found = true;
                    break;
                }
            }

            Log.d(TAG, "addItem: " + name + " rssi:" + rssi);
            Log.d(TAG, "addItem: found:" + found + " index:" + index);

            if (!found) {
                BleScanItem item = new BleScanItem();
                item.device = device;
                item.rssi = rssi;
                item.timestamp = SystemClock.elapsedRealtime();
                devices.add(item);
                notifyItemInserted(devices.size() - 1);
            }else {
                BleScanItem item = devices.get(index);
                item.rssi = rssi;
                long now = SystemClock.elapsedRealtime();
                Log.d(TAG, "addItem: interval :" + ((now - item.timestamp) / 1000));
                if (((now - item.timestamp) / 1000) > 1) {
                    notifyItemChanged(index);
                }
                item.timestamp = now;
            }

        }catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView name;
        private final TextView rssi;

        public ViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.tx_name);
            rssi = view.findViewById(R.id.tx_rssi);
        }

        public TextView getNameView() {
            return name;
        }

        public TextView getRssiView() {
            return rssi;
        }
    }
}
